let uEmailInp = document.querySelector("#useremail")
let codeInp = document.querySelector("#code")
let uEmailText = document.querySelector(".emailtext")
let codeText = document.querySelector(".codetext")
let nextEl = document.querySelector(".loginbtn")

//输入框聚焦失焦的效果
uEmailInp.addEventListener("blur", uEmailInpBlur)
uEmailInp.addEventListener("focus", uEmailInpFocus)
codeInp.addEventListener("blur", codeInpBlur)
codeInp.addEventListener("focus", codeInpFocus)
//从判断验证码是否正确
nextEl.addEventListener("click", next)
// 获取验证码
getCode.addEventListener("click", async function (e) {
    if (judgeEmail() == false){
        judgeRemoveTime()
        return} ;
    getCode.style.color = "#838f8f"
    getCode.disabled = true;
    Countdown(getCode.textContent, 30, getCode, function () {
        getCode.disabled = false
        getCode.style.color = "blue"
    });
    // 发起请求。获取验证码
    let res = await axios({
        method: 'post',
        url: 'http://121.37.252.168:8080/api/user/registerEmail',
        headers: {
            'Content-Type': 'application/json;charset=utf8'
        },
        data: {
            email: uEmailInp.value
        }
    })

    console.log(res);
})

function uEmailInpFocus(e) {
    let inp = e.target
    inp.placeholder = ""
    uEmailText.classList.add("inputclick")
}

function uEmailInpBlur(e) {
    let inp = e.target
    if (inp.value == "") {
        inp.placeholder = "邮箱"
    }
    if (inp.value == "" && inp.placeholder == "邮箱") {

        uEmailText.classList.remove("inputclick")
    }
    // 失去焦点判断邮箱格式
}
function codeInpFocus(e) {
    let inp = e.target
    inp.placeholder = ""
    codeText.classList.add("inputclick")
}

function codeInpBlur(e) {
    let inp = e.target
    if (inp.value == "") {
        inp.placeholder = '请输入验证码'
    }
    if (inp.value == "" && inp.placeholder == '请输入验证码') {

        codeText.classList.remove("inputclick")
    }
}

//从判断验证码是否正确
async function next() {
    //邮箱错误提示框
    if (!judgeEmail()) {
        judgeRemoveTime()
    } 
    // 验证码错误提示框
    else if (!codeInp.value.trim()) {
        judgeCode("请输入验证码")
        judgeRemoveTime()
    } 
    // 如果输入验证码，单独发送请求，判断验证码是不是正确
    else {
        let res = await axios({
            method: 'get',
            url: `http://121.37.252.168:8080/api/user/verifyEmailCode?email=${uEmailInp.value}&verifyCode=${codeInp.value}`,
        });
        let data = res.data;
        // 如果不正确提示验证码错误
        if (data.code != 200) {
            judgeCode(data.msg)
            judgeRemoveTime()
            return
        }
        // 验证码正确跳转下个页面
        else{
            window.location.href=`../public/userinf.html?token=${data.data.token}`
        }

    }

}



